<?php
/**
 *
 * @Author nikm $
 *
 * @Date 2012-05-03 $
 */

if(!defined('FROM_INDEX')) exit('Invalid request');

require ROOT_PATH.'servlet/module/master.mod.php';

class moduleObj extends masterObj
{
	function __construct($request)
	{
		parent::__construct($request);

		$this->excute();
	}

	private function excute()
	{
		switch($this->act)
		{
			case 'list':
				$this->getList();
				break;
			case 'add':
				$this->addFeedback();
				break;
			case 'info':
				$this->getInfo();
				break;
			case 'post_bad_good':
				$this->postBadGood();
				break;
			default:
				$this->message('g_04', STATUS_E);
				break;
		}

		$this->showResponse();
	}

	/**
	 * 
	 * 获取文章评论列表
	 * 
	 */
	private function getList()
	{
		$this->request['aid'] = isset($this->request['aid']) ? (int)$this->request['aid'] : '';
		$this->request['type'] = isset($this->request['type']) ? $this->request['type'] : '';
		
		$key = md5("feedback_list_".$this->request['aid']."_".$this->request['type']."_".$this->request['limit']."_".$this->request['offset']);
		
		$results = cache_get($key);
		if($results !== false)
		{
			$this->response = $results;
			return;
		}
		
		$info = get_article_info($this->request['aid']);
		
		if(!isset($info['id']) || $info['id'] < 1) $this->message('ar_01', STATUS_E);
		
		if($info['notpost']) $this->message('fb_02', STATUS_E);
		
		$result = get_feedback_list($this->request);
		$list = $result['list'];
		
		if(!check_array($list)) $this->message('fb_01', STATUS_F);
		
		foreach($list as &$temp)
		{
			$temp['dtime'] = date('Y-m-d H:i:s', $temp['dtime']);
		}
		
		$this->response['status'] = STATUS_S;
		$this->response['total_page'] = (string)ceil($result['total_count'] / $this->request['limit']);
		$this->response['count'] = (string)count($list);
		$this->response['list'] = $list;
		
		cache_set($key, $this->response, 60);
	}
	
	private function getInfo()
	{
		$id = isset($this->request['id']) ? (int)$this->request['id'] : '';
		
		$info = get_feedback_info($id);
		
		if(!isset($info['id']) || $info['id'] < 1) $this->message('fb_01', STATUS_E);
		
		$info['dtime'] = date('Y-m-d H:i:s', $info['dtime']);
		
		$this->response['status'] = STATUS_S;
		$this->response['info'] = $info;
	}
	
	/**
	 * 
	 * 添加评论
	 * 
	 */
	private function addFeedback()
	{
		global $cfg_feedback_forbid;
		if($cfg_feedback_forbid=='Y') $this->message('fb_02', STATUS_E);
		$this->_filter_all();
		
		$aid = isset($this->request['aid']) ? (int)$this->request['aid'] : '';
		$type = isset($this->request['type']) ? $this->request['type'] : '';
		$msg = isset($this->request['message']) ? $this->request['message'] : '';
		
		if(!in_array($type, array('feedback', 'bad', 'good'))) $type = 'feedback';
		
		$info = get_article_info($aid);
		
		if(!isset($info['id']) || $info['id'] < 1) $this->message('ar_01', STATUS_E);
		
		if($info['notpost']) $this->message('fb_02', STATUS_E);
		
		
		$request['aid'] = $aid;
		$request['type_id'] = $info['typeid'];
		$request['arctitle'] = $info['title'];
		$request['ftype'] = $type;
		
		$request['username'] = '游客';
		$request['ischeck'] = '1';
		$request['ip'] = GetIP();
    	$request['dtime'] = time();
    	
    	$request['msg'] = $msg;
    	$request['face'] = 0;
		
		$result = add_feedback($request);
		
		if($result)
		{
			$this->message('g_06', STATUS_S);
		}
		else
		{
			$this->message('g_07', STATUS_F);
		}
	}
	
	/**
	 * 
	 * 支持/反对评论
	 */
	private function postBadGood()
	{
		$id = isset($this->request['id']) ? (int)$this->request['id'] : '';
		$type = (isset($this->request['type']) && $this->request['type'] == 'good') ? 'good' : 'bad';
		
		$info = get_feedback_info($id);
		
		if(!isset($info['id']) || $info['id'] < 1) $this->message('fb_01', STATUS_E);
		
		$count = add_bad_good_count($id, $type);
		
		$this->response['status'] = STATUS_S;
		$this->response['count'] = $count;
	}
	
	private function _filter_all()
	{
		foreach($this->request as $_k => $_v)
	    {
	        ${$_k} = $this->_do_filter($_k,$_v);
	    }
	}
	
	private function _do_filter($fk, &$svar)
	{
	    global $cfg_notallowstr, $cfg_replacestr;
	    
	    if( is_array($svar) )
	    {
	        foreach($svar as $_k => $_v)
	        {
	            $svar[$_k] = _FilterAll($fk,$_v);
	        }
	    }
	    else
	    {
	        if($cfg_notallowstr!='' && preg_match("#".$cfg_notallowstr."#i", $svar))
	        {
	            $this->message('fb_03', STATUS_E);
	        }
	        if($cfg_replacestr!='')
	        {
	            $svar = preg_replace('/'.$cfg_replacestr.'/i', "***", $svar);
	        }
	    }
	    return $svar;
	}
}